package com.example.demo.BookService.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.BookService.impl.BookService;
import com.example.demo.entity.Book;
import com.example.demo.mapper.BookMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Resource
    BookMapper BookMapper;

    /**
     * 上架图书
     * @param book
     */
    public void insert(Book book) {
        BookMapper.insert(book);
    }

    /**
     * 修改图书
     * @param book
     */
    public void updateById(Book book) {
        BookMapper.updateById(book);
    }

    /**
     * 根据id批量删除图书
     * @param ids
     */
    public void deleteBatchIds(List<Integer> ids) {
        BookMapper.deleteBatchIds(ids);
    }

    /**
     * 根据id删除一条图书数据
     * @param id
     */
    public void deleteById(Long id) {
        BookMapper.deleteById(id);
    }

    /**
     * 分页查询图书数据
     * @param pageNum
     * @param pageSize
     * @param search1
     * @param search2
     * @param search3
     * @return
     */
    public Page<Book> selectPage(Integer pageNum, Integer pageSize, String search1, String search2, String search3) {
        LambdaQueryWrapper<Book> wrappers = Wrappers.<Book>lambdaQuery();
        if (StringUtils.isNotBlank(search1)) {
            wrappers.like(Book::getIsbn, search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrappers.like(Book::getName, search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrappers.like(Book::getAuthor, search3);
        }
        return BookMapper.selectPage(new Page<>(pageNum, pageSize), wrappers);
    }
}
